'''
Company: TWL
Author: xue jian
Email: xuejian@kanzhun.com
Date: 2020-10-30 09:33:40
'''
#
# @lc app=leetcode.cn id=463 lang=python3
#
# [463] 岛屿的周长
#

# @lc code=start
class Solution:
    def islandPerimeter(self, grid: List[List[int]]) -> int:
        ans=0
        for i, p in enumerate(grid):
            for j,pp in enumerate(p):
                if pp==1 and i==0:
                    ans+=1
                if pp==1 and i==len(grid)-1:
                    ans+=1
                if pp==1 and j==0:
                    ans+=1
                if pp==1 and j==len(p)-1:
                    ans+=1
                if pp==1 and i>0 and grid[i-1][j]==0:
                    ans+=1
                if pp==1 and i<len(grid)-1 and grid[i+1][j]==0:
                    ans+=1
                if pp==1 and j>0 and grid[i][j-1]==0:
                    ans+=1
                if pp==1 and j<len(p)-1 and grid[i][j+1]==0:
                    ans+=1
        return ans
# @lc code=end

